package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.SysConfig;
import com.cray.intelligenthydrologyback.client.model.AbstractResponseResult;
import com.cray.intelligenthydrologyback.client.model.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/system")
@Api(value = "Sys Config Controller", tags = "参数配置")
public class SystemConfigController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @PutMapping("/config")
    @ApiOperation(httpMethod = "PUT", value = "修改参数配置")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult updateSystemConfig(
            @ApiParam(value = "device", required = true)
            @RequestBody SysConfig sysConfig) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().put()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config")
                        .build(sysConfig)) // 路径变量
                .bodyValue(sysConfig) // 请求体，可以是任何对象
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @PostMapping("/config")
    @ApiOperation(httpMethod = "POST", value = "增加")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> addSystemConfig(
            @RequestBody SysConfig sysConfig) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()  // 使用 POST 请求
                .uri("/system/config")  // 请求的 URL
                .bodyValue(sysConfig) // 请求体，可以是任何对象
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @GetMapping("/config/{configId}")
    @ApiOperation(httpMethod = "GET", value = "根据参数编号获取详细信息")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> getSystemConfig(
            @ApiParam(value = "id", required = true)
            @PathVariable(value = "configId") Long configId) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config/{configId}")
                        .build(configId)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @DeleteMapping("/config/{configIds}")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult deleteSystemConfig(
            @ApiParam(value = "ids", required = true)
            @PathVariable(value = "configIds") Long configIds) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().delete()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config/{configIds}")
                        .build(configIds)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @GetMapping("/config/configKey/{configId}")
    @ApiOperation(httpMethod = "GET", value = "根据参数键名查询参数值")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> getSystemConfigKey(
            @ApiParam(value = "id", required = true)
            @PathVariable(value = "configKey") Long configKey) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config/configKey/{configKey}")
                        .build(configKey)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @PostMapping("/config/export")
    @ApiOperation(httpMethod = "POST", value = "参数导出")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> exportSystemConfig(
            @RequestBody SysConfig sysConfig) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()  // 使用 POST 请求
                .uri("/system/config/export")  // 请求的 URL
                .bodyValue(sysConfig) // 请求体，可以是任何对象
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @GetMapping("/config/list")
    @ApiOperation(httpMethod = "GET", value = "参数分页查询列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> getSystemConfigList() throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config/list")
                        .build()) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @DeleteMapping("/config/refreshCache")
    @ApiOperation(httpMethod = "DELETE", value = "刷新参数缓存")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult deleteRefreshCache() throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().delete()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/config/refreshCache")
                        .build()) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }
}
